<template>
  <div>
    <el-form :model="formdata" label-width="120px">
      <el-form-item label="是否转思极坐标">
        <el-radio-group v-model="formdata.transSG">
          <el-radio :label="true">是</el-radio>
          <el-radio :label="false">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="文件坐标系">
        <el-select v-model="formdata.coordinate" placeholder="请选择坐标系" style="width: 150px">
          <el-option
            v-for="item in selectOpts.coordOpt"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="子午线经度">
        <el-input v-model="formdata.centerLng" style="width: 150px"></el-input>
      </el-form-item>
      <el-form-item label="文件编码格式">
        <el-select v-model="formdata.charset" placeholder="请选择编码格式" style="width: 150px">
          <el-option v-for="item in selectOpts.charsetOpt" :key="item" :label="item" :value="item"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <el-button @click="selectFile">选择区域文件</el-button>
    <el-button @click="uploadFile">上传区域文件并解析</el-button>
    <el-divider></el-divider>
    <el-button @click="draw(3)">绘制分区</el-button>
    <el-button @click="edit(3)">编辑分区</el-button>
    <el-button @click="split(3)">分割分区</el-button>
    <el-divider></el-divider>
    <el-button @click="draw(4)">绘制网格</el-button>
    <el-button @click="edit(4)">编辑网格</el-button>
    <el-button @click="split(4)">分割网格</el-button>
    <el-divider></el-divider>
    <el-button @click="draw(5)">绘制单元</el-button>
    <el-button @click="edit(5)">编辑单元</el-button>
    <el-button @click="split(5)">分割单元</el-button>
    <el-divider></el-divider>
    <el-button @click="renderTree">绘制区域树</el-button>
    <el-button @click="changeBorder">切换边界颜色</el-button>

    <region-panel :show.sync="show" :getTool="getTool" :fileInfo="fileInfo"></region-panel>
  </div>
</template>

<script>
import mixins from '../mixins';
import RegionPanel from './RegionPanel';
export default {
  name: 'RegionTool',
  components: {
    RegionPanel
  },
  props: {
    getMap: {
      type: Function,
      required: true
    },
    getTool: {
      type: Function,
      required: true
    },
    mapLoadSuccess: Boolean
  },
  mixins: [mixins],
  data() {
    return {
      show: false,
      selectOpts: {
        coordOpt: [
          { id: 0, label: '默认', value: 'DEFAULT' },
          { id: 1, label: '大地2000坐标系', value: 'CGCS2000' },
          { id: 2, label: '西安80坐标系', value: 'XA80' },
          { id: 3, label: '北京54坐标系', value: 'BJ54' }
        ],
        charsetOpt: ['UTF-8', 'GBK']
      },
      fileInfo: null,
      formdata: { transSG: true, coordinate: 'CGCS2000', centerLng: '120', charset: 'UTF-8' },
      tempOid: 10,
      isDefaultStyle: true
    };
  },
  watch: {
    mapLoadSuccess(success) {
      if (success) {
        this.render();
      }
    }
  },
  methods: {
    selectFile() {
      this.getTool('RegionTool').selectFile({
        onChangeFile: (e) => {
          console.log('file changed', e);
          this.$message(`已选择文件${e.target.value}`);
        }
      });
    },
    uploadFile() {
      this.getTool('RegionTool')
        .doUpload(this.formdata)
        .then((fileInfo) => {
          console.log('fileInfo', fileInfo);
          this.fileInfo = fileInfo;
          this.show = true;
        });
    },
    render() {
      this.features.forEach((feature) => {
        feature.properties.geometry = feature.geometry;
      });
      this.getTool('RegionTool').render(this.features);
    },
    draw(qyfl) {
      this.getTool('RegionTool').draw(qyfl, {
        onComplete: (e) => {
          console.log('onComplete', e);
          this.features.push(turf.feature(e.geometry, { oid: this.tempOid, qyfl: qyfl }));
          this.tempOid++;
          this.render();
        }
      });
    },
    edit(qyfl) {
      this.getTool('RegionTool').edit(qyfl, {
        onComplete: (e) => {
          console.log('onComplete', e);
          this.features.forEach((feature) => {
            if (feature.properties.oid === e.properties.oid) {
              feature.geometry = e.geometry;
            }
          });
          // this.features.push(turf.feature(e.geometry, {oid: this.tempOid, qyfl: qyfl}));
          // this.tempOid++;
          this.render();
        }
      });
    },
    split(qyfl) {
      this.getTool('RegionTool')
        .split(qyfl)
        .then((result) => {
          console.log(result);
        });
    },
    renderTree() {
      this.getTool('RegionTool').clear();
      let params = {
        schema: 'fzgh',
        qybh: '320000'
      };
      this.getMap()
        .api.PwghGhsjGhfw.zzjg.post_zzjg_queryQylbByQybh({ params: params })
        .then((res) => {
          // eslint-disable-next-line camelcase
          const { resp_code, datas } = res;
          // eslint-disable-next-line camelcase
          if (resp_code === 200) {
            this.getTool('RegionTool').renderRegionTree(
              datas,
              {
                onChange: (e) => {
                  console.log('onChange', e);
                }
              },
              { oid: 19679, qyfl: 3 }
            );
          }
        });
    },
    changeBorder() {
      this.isDefaultStyle = !this.isDefaultStyle;
      let style = this.isDefaultStyle ? 'Default' : 'Satellite';
      this.getTool('RegionTool').changeBorder(style);
    }
  }
};
</script>

<style scoped></style>
